﻿using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using NetMicro.EventBus.Persistences;

namespace NetMicro.EventBus.BackgroundServices
{
    public class MessageFixServiceProvider : IMessageFixServiceProvider
    {
        private IDelayMessageStorage DelayMessageStorage { get; }
        private IRetryMessageStorage RetryMessageStorages { get; }

        private IDeadLetterMessageStorage DeadLetterMessageStorage { get; }
        private ILogger<MessageFixServiceProvider> Logger { get; }
        public MessageFixServiceProvider(ILogger<MessageFixServiceProvider> logger,
            IDelayMessageStorage delayMessageStorage,
            IRetryMessageStorage retryMessageStorage,
            IDeadLetterMessageStorage  deadLetterMessageStorage)
        {
            Logger = logger;
            DelayMessageStorage = delayMessageStorage;
            RetryMessageStorages = retryMessageStorage;
            DeadLetterMessageStorage = deadLetterMessageStorage;
        }
        public Task DelayMessageExcute(CancellationToken cancellationToken)
        {
            Task.Factory.StartNew(async () =>
            {
                while (true)
                {
                    try
                    {
                        await DelayMessageStorage.ExpireTimeUnLockAsync(cancellationToken);
                        await DelayMessageStorage.DeleteExpiresAsync(cancellationToken);
                        await Task.Delay(TimeSpan.FromMinutes(5));
                    }
                    catch (Exception ex)
                    {
                        Logger.LogError(ex, $"延迟消息补偿操作执行异常");
                        await Task.Delay(TimeSpan.FromMinutes(5));
                    }
                }

            }, TaskCreationOptions.LongRunning);

            return Task.CompletedTask;
        }

        public Task RetryMessageExcute(CancellationToken cancellationToken)
        {
            Task.Factory.StartNew(async () =>
            {
                while (true)
                {
                    try
                    {
                        await RetryMessageStorages.ExpireTimeUnLockAsync(cancellationToken);
                        await RetryMessageStorages.DeleteExpiresAsync(cancellationToken);
                        await Task.Delay(TimeSpan.FromMinutes(5));
                    }
                    catch (Exception ex)
                    {
                        Logger.LogError(ex, $"重试消息补偿操作执行异常");
                        await Task.Delay(TimeSpan.FromMinutes(5));
                    }
                }

            }, TaskCreationOptions.LongRunning);

            return Task.CompletedTask;
        }

        public Task DeadLetterMessageExcute(CancellationToken cancellationToken)
        {
            Task.Factory.StartNew(async () =>
            {
                while (true)
                {
                    try
                    {
                        await DeadLetterMessageStorage.DeleteExpiresAsync(cancellationToken);
                        await Task.Delay(TimeSpan.FromHours(1));
                    }
                    catch (Exception ex)
                    {
                        Logger.LogError(ex, $"死信消息补偿操作执行异常");
                        await Task.Delay(TimeSpan.FromHours(1));
                    }
                }

            }, TaskCreationOptions.LongRunning);

            return Task.CompletedTask;
        }
    }
}
